﻿using GBT.Tool.Core;
using Newtonsoft.Json.Linq;
using NLog;
using ReportMealManagementSystem.Entity;
using SpeechLib;
using System;
using System.Configuration;

namespace ReportMealManagementSystem.Business
{
    public class BasicInfo
    {
        private Logger logger = LogManager.GetCurrentClassLogger();
        Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        HttpRequest httpRequest = new HttpRequest();
        SpVoice spVoice = new SpVoice();

        #region 通用方法

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="param"></param>
        /// <param name="url"></param>
        /// <param name="title"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        internal JArray GetData(JObject param, string url, string title, ref int totalNumber, ref int totalPage, ref string message)
        {
            JArray jaData = null;
            try
            {
                string result = "";
                string error = "";
                result = httpRequest.GBTPost(Global.host + url, param.ToString());
                JObject joResult = JObject.Parse(result);
                jaData = (JArray)joResult["data"];
                totalNumber = (int)joResult["totalNumber"];
                totalPage = (int)joResult["totalPage"];
                error = (string)joResult["Error"];
                if (!string.IsNullOrEmpty(error))
                {
                    logger.Error(error);
                    message = "获取" + title + "信息出错，详情请查看日志。";
                }
                else
                {
                    message = "获取" + title + "信息成功，共 " + totalNumber + " 条记录。";
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return jaData;
        }

        /// <summary>
        /// 新增，修改，删除信息
        /// </summary>
        /// <param name="joParam"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        internal bool OptDataInfo(JObject joParam, string url, ref string message)
        {
            bool state = false;
            try
            {
                string param = joParam.ToString();
                string result = "";
                result = httpRequest.GBTPost(Global.host + url, param);
                JObject joResult = JObject.Parse(result);
                JObject joRoot = (JObject)joResult["root"];
                state = (bool)joRoot["state"];
                message = (string)joRoot["message"];
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return state;
        }

        /// <summary>
        /// 新增，修改，删除信息
        /// </summary>
        /// <param name="joParam"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        internal bool OptDataInfoNew(JObject joParam, string url, ref string message)
        {
            bool state = false;
            try
            {
                string param = joParam.ToString();
                string result = "";
                result = httpRequest.GBTPost(Global.host + url, param);
                JObject joResult = JObject.Parse(result);
                int resultLine = (int)joResult["root"];
                if (resultLine > 0)
                {
                    state = true;
                }
                message = (string)joResult["msg"];
                if (!string.IsNullOrEmpty((string)joResult["error"]))
                {
                    logger.Error((string)joResult["error"]);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return state;
        }

        /// <summary>
        /// 获取版本信息
        /// </summary>
        internal JArray GetVersionsInfo()
        {
            JArray jaData = null;
            try
            {
                string result = "";
                result = httpRequest.GBTGet(Global.host + "gbt/BasicInfo/GetVersionInfo");
                JObject joResult = JObject.Parse(result);
                JArray jaRoot = (JArray)joResult["root"];
                jaData = jaRoot;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return jaData;
        }

        /// <summary>
        /// 获取系统配置参数
        /// </summary>
        internal JObject GetSystemSettingInfo(int districtID)
        {
            JObject joData = null;
            try
            {
                string result = "";
                result = httpRequest.GBTGet(Global.host + "gbt/SystemSetting/SystemSetting/" + districtID);
                JObject joResult = JObject.Parse(result);
                joData = (JObject)joResult["root"];
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return joData;
        }

        internal void Speak(string speechSounds)
        {
            spVoice.Speak(speechSounds, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }
        #endregion
    }
}
